<?php
/**
 * Created by PhpStorm.
 * User: hgs
 * Date: 16/3/30
 * Time: 下午2:38
 */

namespace C\mod {

    use C\lib\Db;
    use C\lib\Sms;
    use C\lib\Text;

    class user extends mod {

        protected static $__table_s = 'user';
        protected static $__primary_s = 'id';

        // --- 验证手机验证码 ---
        public static function sendVerifyMessage($phone, $app = '去外地') {

            $code = Text::random(6, ['N']);
            $pre = Db::prepare('INSERT'.' INTO '.DB_PRE.'phone_verify (phone, code, time_add, url) SELECT :phone, :code, :time_add, :url FROM DUAL WHERE NOT EXISTS(SELECT * FROM '.DB_PRE.'phone_verify WHERE phone = :phone AND time_add >= "'.($_SERVER['REQUEST_TIME'] - 60).'");');
            $pre->execute([
                ':phone' => $phone,
                ':code' => $code,
                ':time_add' => $_SERVER['REQUEST_TIME'],
                ':url' => URI
            ]);
            if($pre->rowCount() > 0) {
                Sms::send($phone, '【'.$app.'】您的验证码是' . $code . '，请在10分钟内完成验证，不要告知他人。');
                return true;
            } else
                return false;

        }

        // --- 绑定登录 ---
        public static function verifyMessage($phone, $code) {
            $pre = Db::prepare('DELETE'.' FROM '.DB_PRE.'phone_verify WHERE phone = :phone AND code = :code AND time_add >= :time_add');
            $pre->execute([
                ':phone' => $phone,
                ':code' => $code,
                ':time_add' => $_SERVER['REQUEST_TIME'] - 600
            ]);
            if($pre->rowCount() > 0) {
                return true;
            } else
                return false;
        }

    }

}

